//
//  YGKPointRecordController.m
//  PathTrack
//
//  Created by YGK on 16/7/5.
//  Copyright © 2016年 YGK. All rights reserved.
//

#import "YGKPointRecordController.h"


@interface YGKPointRecordController ()<CLLocationManagerDelegate>
- (IBAction)goBackAction:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *flagIDTextField;
@property (weak, nonatomic) IBOutlet UITextField *typeTextField;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
- (IBAction)saveAndUploadAction:(id)sender;

@property (nonatomic,strong) CLLocationManager *locationManager;

@end

@implementation YGKPointRecordController

-(CLLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        [_locationManager requestAlwaysAuthorization];
    }
    return _locationManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.typeTextField resignFirstResponder];
    [self.flagIDTextField resignFirstResponder];
}

- (IBAction)saveAndUploadAction:(id)sender {
   
    if ([self.flagIDTextField.text isEqualToString:@""]|| [self.flagIDTextField.text length]==0||[self.typeTextField.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"请输入完整信息" message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles: nil];
        [alertView show];
        
    } else{
        
        if ([self.typeTextField.text integerValue]==1||[self.typeTextField.text integerValue]==2) {
            TargetPoint * targetPoint = [NSEntityDescription insertNewObjectForEntityForName:@"TargetPoint" inManagedObjectContext:[HMDataManager sharedManager].managedObjectContext];
            targetPoint.flag_id = self.flagIDTextField.text;
            targetPoint.type = [NSNumber numberWithInteger:[self.typeTextField.text integerValue]];
            targetPoint.x_code = [NSNumber numberWithFloat:[self.longitudeLabel.text floatValue]];
            targetPoint.y_code = [NSNumber numberWithFloat:[self.latitudeLabel.text floatValue]];
            [[HMDataManager sharedManager] saveContext];
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"保存成功！" message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles: nil];
            [alertView show];
            self.flagIDTextField.text = nil;
            self.typeTextField.text = nil;
        } else {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"type类型只能输入1或2" message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles: nil];
            [alertView show];
        }
        
    }
    
}

- (IBAction)goBackAction:(id)sender {
    [self.locationManager stopUpdatingLocation];
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark CLLocationDelegate 方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *location = locations.firstObject;
    self.latitudeLabel.text = [NSString stringWithFormat:@" %.6f",location.coordinate.latitude];
    self.longitudeLabel.text = [NSString stringWithFormat:@"%.6f",location.coordinate.longitude];
}

@end









